package jgl.vajra.operator.part.mine.activity;

import androidx.recyclerview.widget.GridLayoutManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.HomeCarModelBean;
import jgl.vajra.operator.bean.HomeMechanicsBean;
import jgl.vajra.operator.databinding.ActivityCarAddBinding;
import jgl.vajra.operator.dialogFragment.CustomCarTypeDialogFragment;
import jgl.vajra.operator.listener.MyToolbarViewListener;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.mine.contract.CarAddContract;
import jgl.vajra.operator.part.mine.viewModel.CarAddViewModel;
import jgl.vajra.operator.utils.RandomUtils;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.retrofithelper.utils.AEACCoder;

@CreateViewModel(CarAddViewModel.class)
public class CarAddActivity extends BaseMVVMActivity<CarAddViewModel, ActivityCarAddBinding> implements CarAddContract.View, BaseBindingItemPresenter<HomeMechanicsBean> {

    private SingleTypeBindingAdapter adapterMechanics;
    private List<HomeMechanicsBean> homeMechanicsBean;
    private SingleTypeBindingAdapter adapter;
    private CustomCarTypeDialogFragment customCarTypeDialogFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_add;
    }

    @Override
    public void initView() {
        super.initView();

        mBinding.toolbar.setMyToolbarViewListener(new MyToolbarViewListener(){
            @Override
            public void onRightTextClick() {
                super.onRightTextClick();
                HomeMechanicsBean itemData =new HomeMechanicsBean();
                IntentManager.toCarInformationActivity(CarAddActivity.this,itemData);
            }
        });


        //豪车类型
        // 不可嵌套滑动
        mBinding.recyclerViewType.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewType.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewType.setLayoutManager(new GridLayoutManager(this, 3));
        adapterMechanics = new SingleTypeBindingAdapter(this, null, R.layout.item_car_type);
        mBinding.recyclerViewType.setAdapter(adapterMechanics);
        adapterMechanics.setItemPresenter(this);


        requestNetData();
    }

    public void requestNetData() {

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));//随机数

        mViewModel.getCarTypeData(map);
    }

    //自定义车型
    @Override
    public void onCustom() {

        customCarTypeDialogFragment = new CustomCarTypeDialogFragment();
        customCarTypeDialogFragment.show(getSupportFragmentManager());

    }

    //选择车的型号
    @Override
    public void onCarType(int position, HomeCarModelBean itemData) {

    }

    @Override
    public void returnCarTypeBean(List<HomeMechanicsBean> homeMechanicsBean) {
        this.homeMechanicsBean = homeMechanicsBean;
        if (homeMechanicsBean != null && homeMechanicsBean.size() != 0) {
            homeMechanicsBean.get(0).isClick = true;
        }
        adapterMechanics.refresh(homeMechanicsBean);
    }

    @Override
    public void returnHomeCarModelBean(List<HomeCarModelBean> data) {
        adapter.refresh(data);
    }

    private void getCarModelData(String type) {
        //机械型号
        Map<String, Object> mapCarModel = new HashMap<>();
        mapCarModel.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        mapCarModel.put("type", type);
        mViewModel.getCarModelData(mapCarModel);
    }

    //类型
    @Override
    public void onItemClick(int position, HomeMechanicsBean itemData) {
        IntentManager.toCarInformationActivity(this,itemData);
    }


}
